﻿/* 2011/11/26 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using Cosmos.Shortcuts;

namespace PictureManager
{
    public partial class MouseGestureControl : UserControl
    {
        public MouseGestureControl()
        {
            InitializeComponent();

            foreach (ShortcutOperationInformation anOperation in PictureManagerShortcutManager.AvailableOperations)
                operationComboBox.Items.Add(new OperationItem((PictureManagerOperationCategory)anOperation.Operation, anOperation.OperationDescription));

            mouseGestureListView.ListViewItemSorter = new ShortcutActionItemSorter();
        }

        public void LoadSettings(Color trackColor, float trackWidth, StringCollection mouseGestureExecutants)
        {
            trackColorLabel2.BackColor = trackColor;
            trackWidthNumericUpDown.Value = (decimal)trackWidth;
            mouseGestureListView.Items.Clear();
            foreach (string anExecutant in mouseGestureExecutants)
            {
                try
                {
                    mouseGestureListView.Items.Add(new MouseGestureListViewItem(anExecutant));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void SaveSettings(out Color trackColor, out float trackWidth, StringCollection mouseGestureExecutants)
        {
            trackColor = trackColorLabel2.BackColor;
            trackWidth = (float)trackWidthNumericUpDown.Value;
            mouseGestureExecutants.Clear();
            foreach (ListViewItem anItem in mouseGestureListView.Items)
            {
                MouseGestureListViewItem theItem = (MouseGestureListViewItem)anItem;
                mouseGestureExecutants.Add(theItem.EscapeString);
            }
        }

        private void operationAddButton_Click(object sender, EventArgs e)
        {
            if (operationComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择操作", "添加失败");
                return;
            }

            OperationItem theOperationItem = (OperationItem)operationComboBox.SelectedItem;
            MouseGestureListViewItem theMouseGestureItem;
            switch (PictureManagerShortcutManager.GetParameterCount(theOperationItem.Operation))
            {
                case 0:
                    theMouseGestureItem = new MouseGestureListViewItem(theOperationItem.Operation);
                    break;
                case 1:
                    theMouseGestureItem = new MouseGestureListViewItem(theOperationItem.Operation, operationFirstArgumentTextBox.Text);
                    break;
                case 2:
                    theMouseGestureItem = new MouseGestureListViewItem(theOperationItem.Operation, operationFirstArgumentTextBox.Text, operationSecondArgumentTextBox.Text);
                    break;
                default: theMouseGestureItem = null; break;
            }
            if (!PictureManagerShortcutManager.IsParameterLegal(
                new ShortcutOperation((int)theMouseGestureItem.Operation, theMouseGestureItem.Arguments)))
            {
                MessageBox.Show("参数不符合要求", "添加失败");
                return;
            }
            mouseGestureListView.Items.Add(theMouseGestureItem);
            foreach (ListViewItem anItem in mouseGestureListView.SelectedItems)
            {
                anItem.Selected = false;
            }
            theMouseGestureItem.Selected = true;
            theMouseGestureItem.EnsureVisible();
        }

        private void operationRemoveButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = mouseGestureListView.SelectedItems;
            if (items.Count > 0)
                foreach (ListViewItem aItem in items)
                    mouseGestureListView.Items.Remove(aItem);
            else
                MessageBox.Show("请选择要删除的操作", "删除失败");
        }

        private void operationModifyButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = mouseGestureListView.SelectedItems;
            if (items.Count != 1)
            {
                MessageBox.Show("请选择一个要修改的操作", "修改失败");
                return;
            }

            if (operationComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择操作", "修改失败");
                return;
            }

            OperationItem theOperationItem = (OperationItem)operationComboBox.SelectedItem;
            ShortcutOperation shortcutOperation;
            switch (PictureManagerShortcutManager.GetParameterCount(theOperationItem.Operation))
            {
                case 0:
                    shortcutOperation = new ShortcutOperation((int)theOperationItem.Operation);
                    break;
                case 1:
                    shortcutOperation = new ShortcutOperation((int)theOperationItem.Operation, operationFirstArgumentTextBox.Text);
                    break;
                case 2:
                    shortcutOperation = new ShortcutOperation((int)theOperationItem.Operation, operationFirstArgumentTextBox.Text, operationSecondArgumentTextBox.Text);
                    break;
                default: shortcutOperation = new ShortcutOperation(); break;
            }
            if (!PictureManagerShortcutManager.IsParameterLegal(shortcutOperation))
            {
                MessageBox.Show("参数不符合要求", "修改失败");
                return;
            }
            ((MouseGestureListViewItem)mouseGestureListView.SelectedItems[0]).ShortcutOperation = shortcutOperation;
        }

        private void mouseGestureAppendButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = mouseGestureListView.SelectedItems;
            if (items.Count != 1)
            {
                MessageBox.Show("请选择一个要添加鼠标手势的操作", "添加失败");
                return;
            }

            CaptureMouseGestureForm form = new CaptureMouseGestureForm();
            form.ShowDialog();
            MouseGesture capturedMouseGesture = form.Gesture;

            if (capturedMouseGesture.DirectionCount == 0)
            {
                MessageBox.Show("鼠标手势无效", "添加失败");
            }
            else
            {
                foreach (ListViewItem anItem in mouseGestureListView.Items)
                {
                    MouseGestureListViewItem theItem = (MouseGestureListViewItem)anItem;
                    if (theItem.Contains(capturedMouseGesture))
                    {
                        MessageBox.Show("此手势已经被使用", "添加失败");
                        return;
                    }
                }
                ((MouseGestureListViewItem)items[0]).Add(capturedMouseGesture);
            }

            form.Dispose();
        }

        private void mouseGestureClearButton_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection items = mouseGestureListView.SelectedItems;
            foreach (ListViewItem anItem in items)
                ((MouseGestureListViewItem)anItem).Clear();
        }

        private void trackColorSetButton_Click(object sender, EventArgs e)
        {
            if (trackColorDialog.ShowDialog() == DialogResult.OK)
            {
                trackColorLabel2.BackColor = trackColorDialog.Color;
            }
        }

        private void mouseGestureListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                MouseGestureListViewItem theItem = (MouseGestureListViewItem)e.Item;
                foreach (object aItem in operationComboBox.Items)
                {
                    if (((OperationItem)aItem).Operation == theItem.Operation)
                    {
                        operationComboBox.SelectedItem = aItem;
                        break;
                    }
                }
                switch (theItem.Arguments.Length)
                {
                    case 0:
                        break;
                    case 1:
                        operationFirstArgumentTextBox.Text = theItem.Arguments[0];
                        break;
                    case 2:
                        operationFirstArgumentTextBox.Text = theItem.Arguments[0];
                        operationSecondArgumentTextBox.Text = theItem.Arguments[1];
                        break;
                    default: break;
                }                
            }
        }

        private void operationComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            OperationItem theItem = (OperationItem)operationComboBox.SelectedItem;
            argumentIntroductionLabel.Text = PictureManagerShortcutManager.GetParameterDescription(theItem.Operation);
            switch (PictureManagerShortcutManager.GetParameterCount(theItem.Operation))
            {
                case 0:
                    operationFirstArgumentTextBox.Text = String.Empty; operationFirstArgumentTextBox.Enabled = false;
                    operationSecondArgumentTextBox.Text = String.Empty; operationSecondArgumentTextBox.Enabled = false;
                    break;
                case 1:
                    operationFirstArgumentTextBox.Enabled = true;
                    operationSecondArgumentTextBox.Text = String.Empty; operationSecondArgumentTextBox.Enabled = false;
                    break;
                case 2:
                    operationFirstArgumentTextBox.Enabled = true;
                    operationSecondArgumentTextBox.Enabled = true;
                    break;
                default: break;
            }
        }
    }

}
